/**
 * 
 */
package com.agilegroup.crm.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.agilegroup.crm.AgileApplication;
import com.agilegroup.crm.adapters.ViewDetailsAdapter;
import com.agilegroup.crm.model.Contact;
import com.agilegroup.crm.model.EntryItem;
import com.agilegroup.crm.model.Item;
import com.agilegroup.crm.model.SectionItem;
import com.agilegroup.crm.requests.ViewContactJsonRequest;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;

/**
 * @author Hisham
 *
 */
public class ViewContactActivity extends SherlockListActivity {
    /** Called when the activity is first created. */
	private ArrayList<Item> items = new ArrayList<Item>();
	private static final String JSON_CACHE_KEY = "view_contact_json";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String contactId = intent.getStringExtra("contactId");
        AgileApplication.getInstance().spiceManager.execute(new ViewContactJsonRequest(contactId),JSON_CACHE_KEY,DurationInMillis.ALWAYS_EXPIRED,new ViewContactJsonRequestListener());
    }
    
    public class ViewContactJsonRequestListener implements RequestListener<Contact>{

		public void onRequestFailure(SpiceException spiceException) {
			Toast.makeText(ViewContactActivity.this, "Failure", Toast.LENGTH_SHORT).show();
		}
		public void onRequestSuccess(Contact contact) {
			items.add(new SectionItem("Required Information"));
			if(contact.getCreatorName()!=null&&!contact.getCreatorName().equalsIgnoreCase("")){
				items.add(new EntryItem("Contact Owner", contact.getCreatorName()));
			}
			else{
				items.add(new EntryItem("Contact Owner", "  "));
			}
			if(contact.getfName()!=null&&!contact.getfName().equalsIgnoreCase("")){
				items.add(new EntryItem("First Name", contact.getfName()));
			}
			else{
				items.add(new EntryItem("First Name", "  "));
			}
			if(contact.getlName()!=null&&!contact.getlName().equalsIgnoreCase("")){
				items.add(new EntryItem("Last Name", contact.getlName()));
			}
			else{
				items.add(new EntryItem("Last Name", "   "));
			}
			if(contact.getParentName()!=null&&!contact.getParentName().equalsIgnoreCase("")){
				 items.add(new EntryItem("Lead/Account Name", contact.getAccountName()));
			}
			else{
				items.add(new EntryItem("Lead/Account Name", "   "));
			}
			if(contact.getCompanyName()!=null&&!contact.getCompanyName().equalsIgnoreCase("")){
				items.add(new EntryItem("Company Name", contact.getCompanyName()));
			}
			else{
				items.add(new EntryItem("Company Name", "   "));
			}
	        
	        items.add(new SectionItem("Contact Information"));
	        if(contact.getTitle()!=null&&!contact.getTitle().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Title", contact.getTitle()));
	        }
	        else{
				items.add(new EntryItem("Title", "   "));
			}
	        if(contact.getPhone()!=null&&!contact.getPhone().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Phone", contact.getPhone()));
	        }
	        else{
				items.add(new EntryItem("Phone", "   "));
			}
	        if(contact.getMobile()!=null&&!contact.getMobile().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Mobile", contact.getMobile()));
	        }
	        else{
				items.add(new EntryItem("Mobile", "   "));
			}
	        if(contact.getEmail()!=null&&!contact.getEmail().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Email", contact.getEmail()));
	        }
	        else{
				items.add(new EntryItem("Email", "   "));
			}
	        ViewDetailsAdapter adapter = new ViewDetailsAdapter(ViewContactActivity.this, items);
	        setListAdapter(adapter);
		}
	 }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	int itemId = item.getItemId();
        switch (itemId) {
        case android.R.id.home:
             finish();
            break;

        }

        return true;
    }
}